<?php
/**
 * User: 五哥派
 * Date: 2024/6/25
 * Tel(同WX): 17335592719
 */

namespace app\admin\model;

use think\Model;

class Base extends Model{

    //每页条数
    protected $limit = 10;

    protected $autoWriteTimestamp = true;

    protected $order = ["id" => "desc"];


    public function total(){
        return self::count();
    }

    public function getList($where=array()){
        return self::where($where)->order($this->order)->paginate($this->limit, false);
    }

    public function queryData($where=array(), $field='*'){
        return self::field($field)->where($where)->select();
    }

    public function findDataByID($id){
        return self::where('id',$id)->find();
    }

    public function findData($where = array()){
        return self::where($where)->find();
    }

    public function saveData($data, $isUpdate, $where = array()){
        return self::allowField(true)->isUpdate($isUpdate)->save($data, $where);
    }

    public function updateById($id, $data = array()) {
        $data['update_time'] = time();

        return self::where(["id" => $id])->update($data);

    }

    //根据条件删除
    public function delDataByWhere($where = array()){
        return self::destroy($where);
    }

    //根据id删除
    public function delDataByID($id){
        return self::destroy($id);
    }

    protected function prefixImageUrl($value){
        return config("setting.domain_prefix").config("setting.image_prefix").$value;
    }

    public static function detail($where, array $with = [])
    {
        try {
            $query = (new static)->with($with);
            return is_array($where) ? $query->where($where)->find() : $query->find((int)$where);
        } catch (\Exception $e) {
            return null;
        }
    }

}